package com.lb.shopguide.ui.fragment.boss.market;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.adapter.AdapterActivityAdvice;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.entity.market.ActivityAdviceBean;
import com.lb.shopguide.event.boss.AdviceRefreshEvent;
import com.lb.shopguide.http.method.ApiMethodBoss;
import com.lb.shopguide.ui.activity.GuideMainActivity;
import com.lb.shopguide.ui.view.NormalTitleBar;
import com.lb.shopguide.util.json.JsonUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class FragmentActivityAdvice extends BaseCommonFragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.layout_root)
    LinearLayout layoutRoot;

    private List<ActivityAdviceBean> mActivityAdviceList;

    private AdapterActivityAdvice mAdapterActivityAdvice;

    public static FragmentActivityAdvice newInstance() {
        return new FragmentActivityAdvice();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_activity_advice;
    }

    @Override
    public void initData() {
        mActivityAdviceList = new ArrayList<>();
    }

    @Override
    public void initView() {
        ntb.setTitleText("活动建议");
        ntb.setLeftImageSrc(R.drawable.iv_back);
        ntb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeColors(Color.rgb(253, 91, 89));

        noDataView = LayoutInflater.from(mContext).inflate(R.layout.layout_empty_view, (ViewGroup) mRecyclerView.getParent(), false);
        TextView tvEmpty = noDataView.findViewById(R.id.tv_empty);
        tvEmpty.setText("暂时没有活动建议");

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapterActivityAdvice = new AdapterActivityAdvice(R.layout.item_activity_advice, mActivityAdviceList);
        mRecyclerView.setAdapter(mAdapterActivityAdvice);

        initListener();
        sendRequestGetAdvices();
        sendRequestReadAdvices();
    }

    private void initListener() {
        mAdapterActivityAdvice.setOnUseAdviceListener(new AdapterActivityAdvice.OnUseAdviceListener() {
            @Override
            public void onUseAdvice(String storeCode, String templateCode, String templateName) {
                start(FragmentUseTemplate.newInstance(templateCode, templateName, storeCode, null));
            }
        });
    }

    @Override
    public void finishRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void sessionOutOfDate() {
        ((GuideMainActivity) getActivity()).onSessionOutOfDate();
    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
    }

    @Override
    public String getPageTag() {
        return FragmentActivityAdvice.class.getName();
    }

    @Override
    public void onRefresh() {
        mAdapterActivityAdvice.setEnableLoadMore(false);
        if (mError) {
            mAdapterActivityAdvice.setEmptyView(errorView);
            mError = false;
        } else {
            if (mNoData) {
                mAdapterActivityAdvice.setEmptyView(noDataView);
                mNoData = false;
            } else {
                refresh();
            }
        }
    }


    public void refresh() {
        mActivityAdviceList = new ArrayList<>();
        sendRequestGetAdvices();
    }

    private void sendRequestGetAdvices() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    if (response.getReturnContent() != null) {
                        JsonArray jsonArrayActivityAdvices = JsonUtil.getJsonArrayFromMap(response.getReturnContent());
                        if (jsonArrayActivityAdvices.size() != 0) {
                            for (JsonElement obj : jsonArrayActivityAdvices) {
                                ActivityAdviceBean bean = JsonUtil.getGson().fromJson(obj, ActivityAdviceBean.class);
                                mActivityAdviceList.add(bean);
                            }
                            mAdapterActivityAdvice.setNewData(mActivityAdviceList);
                            layoutRoot.setBackgroundColor(getResources().getColor(R.color.bs_red_temp));
                        } else {
                            layoutRoot.setBackgroundColor(getResources().getColor(R.color.white));
                            bindEmpty();
                        }
                        finishRefresh();
                    }
                }
            }
        };
        ApiMethodBoss.listActivitySuggestion(new SimpleLbObserver(listener, otherListener), this, FragmentEvent.DESTROY_VIEW);

    }

    private void sendRequestReadAdvices() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    EventBus.getDefault().post(new AdviceRefreshEvent());
                }
            }
        };
        ApiMethodBoss.readActivitySuggestion(new SimpleLbObserver(listener, otherListener), null, this, FragmentEvent.DESTROY_VIEW);
    }

    private void bindEmpty() {
        mActivityAdviceList.clear();
        mAdapterActivityAdvice.setNewData(mActivityAdviceList);

        mNoData = true;
        onRefresh();
    }

}
